﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcCarConsume.Domain.Entites;
using System.Text;

namespace MvcCarConsume.WebUI
{
    //model for CarView/Detail
    public class CarInView
    {
        public int CarID { get; set; }
        public string CarName { get; set; }
        public decimal CarPrice { get; set; }
        public string CarDescription { get; set; }
        public int CarCount { get; set; }

        public CarInView()
        {
            CarDescription = string.Empty;
        }

        public void LoadCar(int carId, string carName, decimal carPrice, string carDescription, int carCount)
        {
            CarID = carId;
            CarName = carName;
            CarPrice = carPrice;
            CarDescription = carDescription;
            CarCount = carCount;
        }

    }

    public class PriceInterval
    {
        public PriceInterval(decimal l , decimal h)
        {
            low = l;
            high = h;
        }
        public decimal low{get;set;}
        public decimal high{get;set;}

    }

    public class BrandSelector
    {
        public int id{get;set;}
        public string name{get;set;}
        public BrandSelector()
        {}
        public BrandSelector(int id,string name)
        {
            this.id = id;
            this.name = name;
        }
    }

    public class VMPageInfo
    {
        public int TotalItems { get; set; }
        public int CurrentPage { get; set; }
        public int ItemsPerPage { get; set; }

        public int TotalPage
        {
            get { return (int)Math.Ceiling((decimal)TotalItems / ItemsPerPage); }
        }
    }


    public class VMCarQueryInfo
    {
        //current selectcategory
        public CategoryInView SelectedCategory { get; set; }
        public BrandSelector Brand { get; set; }
        public decimal BeginPrice { get; set; }
        public decimal EndPrice { get; set; }
        //current pageinfo
        public VMPageInfo PageInfo { get; set; }

        public VMCarQueryInfo()
        {
            SelectedCategory = new CategoryInView();
            PageInfo = new VMPageInfo();
            Brand = new BrandSelector();
        }
    }

    //model for CarView/Index
    public class VMCarViewIndex
    {
        public IList<CarInView> Cars { get; set; }
        public VMCarQueryInfo QueryInfo { get; set; }
        public IEnumerable<BrandSelector> brands { get; set; }
        public IEnumerable<PriceInterval> prices { get; set; }
        public VMCarViewIndex()
        {
            Cars = new List<CarInView>();
            QueryInfo = new VMCarQueryInfo();
            prices = new List<PriceInterval>() { 
                new PriceInterval(0,0),//all
                new PriceInterval(1,40),
                new PriceInterval(41,70),
                new PriceInterval(71,100),
                new PriceInterval(101,0)//21 above
            };
            brands = new List<BrandSelector>();
        }

        public void LoadCategory(MvcCarConsume.Domain.Entites.Type type)
        {
            if (type == null)
            {
                QueryInfo.SelectedCategory.CategoryName = "全部";
                QueryInfo.SelectedCategory.CategoryID = 0;
                QueryInfo.SelectedCategory.CategoryDescription = "";
            }
            else
            {
                QueryInfo.SelectedCategory.CategoryName = type.TypeName;
                QueryInfo.SelectedCategory.CategoryID = type.TypeId;
                QueryInfo.SelectedCategory.CategoryDescription = type.Description;
            }
        }

        public void LoadCars(IEnumerable<Car> list , int page , int pageSize)
        {
            if (list != null)
            {
                int count = list.Count();
                foreach (var l in list.Skip(pageSize * (page - 1)).Take(pageSize))
                {
                    Cars.Add(new CarInView
                    {
                        CarID = l.CarId,
                        CarName = l.CarName,
                        CarPrice = l.Price,
                        CarDescription = l.Description,
                        CarCount = l.Count
                    });
                }
                QueryInfo.PageInfo.CurrentPage = page;
                QueryInfo.PageInfo.TotalItems = count;
                QueryInfo.PageInfo.ItemsPerPage = pageSize;
            }

        }

        public void LoadPriceInterval(decimal lp, decimal hp)
        {
            QueryInfo.BeginPrice = lp;
            QueryInfo.EndPrice = hp;
        }

        public void LoadBrandSelectors(IEnumerable<Brand> brands , int currentbrandid)
        {
            var temp = this.brands as List<BrandSelector>;
            temp.Add(new BrandSelector(0,"key_all"));
            if (brands != null)
            {
                foreach (var b in brands)
                    temp.Add(new BrandSelector(b.BrandId, b.BrandName));
            }
            QueryInfo.Brand.id = currentbrandid;
        }

        public string BuildQueryStringForBrand(int id)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(QueryInfo.SelectedCategory.CategoryID);
            sb.Append("-");
            sb.Append(id);
            sb.Append("-");
            sb.Append(QueryInfo.BeginPrice);
            sb.Append("-");
            sb.Append(QueryInfo.EndPrice);
            sb.Append("-");
            sb.Append(QueryInfo.PageInfo.CurrentPage);

            return sb.ToString();
        }

        public string BuildQueryStringForPrice(decimal low , decimal high)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(QueryInfo.SelectedCategory.CategoryID);
            sb.Append("-");
            sb.Append(QueryInfo.Brand.id);
            sb.Append("-");
            sb.Append(low);
            sb.Append("-");
            sb.Append(high);
            sb.Append("-");
            sb.Append(QueryInfo.PageInfo.CurrentPage);

            return sb.ToString();
        }

        public object BuildPriceLinkAttr(PriceInterval pi)
        {
            if (pi.low == QueryInfo.BeginPrice && pi.high == QueryInfo.EndPrice)
            {
                return new { @class = "selected" };
            }
            return null;
        }

        public object BuildBrandLinkAttr(int id)
        {
            if (QueryInfo.Brand.id == id)
            {
                return new { @class = "selected" };
            }
            return null;
        }

        public string BuildQueryStringForPage(int page)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(QueryInfo.SelectedCategory.CategoryID);
            sb.Append("-");
            sb.Append(QueryInfo.Brand.id);
            sb.Append("-");
            sb.Append(QueryInfo.BeginPrice);
            sb.Append("-");
            sb.Append(QueryInfo.EndPrice);
            sb.Append("-");
            sb.Append(page);

            return sb.ToString();
        }


    }
}
